package com.fruit.api.controller.fruit;

import com.fruit.api.controller.BaseController;
import com.fruit.bean.constant.factory.PageFactory;
import com.fruit.bean.dto.fruit.FruitCommentDto;
import com.fruit.bean.entity.fruit.FruitCommentInfo;
import com.fruit.bean.entity.fruit.FruitUserOrderInfo;
import com.fruit.bean.enumeration.fruit.FruitUserOrderStatusEnum;
import com.fruit.bean.vo.front.Rets;
import com.fruit.service.fruit.FruitCommentService;
import com.fruit.service.fruit.FruitUserOrderService;
import com.fruit.utils.BeanUtil;
import com.fruit.utils.factory.Page;
import com.fruit.warpper.UserWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 水果评论相关接口
 */
@RestController
@RequestMapping("/fruit/comment")
public class FruitCommentController extends BaseController {
    @Autowired
    private FruitCommentService commentService;

    @Autowired
    private FruitUserOrderService orderService;

    /**
     * 管理端查询评论列表，支持按照货架查询
     * @param shelfId 货架id
     * @return
     */
    @PostMapping("/list")
    public Object listComment(@RequestParam(required = false) Long shelfId){
        Page page = new PageFactory().defaultPage();
        page.addFilter("fruitShelfId", shelfId);
        page = commentService.queryPage(page);
        List list = (List) new UserWarpper(BeanUtil.objectsToMaps(page.getRecords())).warp();
        page.setRecords(list);
        return Rets.success(page);
    }

    /**
     * 小程序端查询评论列表，支持按照货架查询
     * @param  shelfInfo shelfId 货架id
     * @return
     */
    @PostMapping("/app/list")
    public Object listFruitComment(@RequestBody Map<String,Long> shelfInfo){
        List<FruitCommentDto> list = commentService.listFruitComment(shelfInfo.get("shelfId"));
        return Rets.success(list);
    }

    /**
     * 新增或修改评论
     * @param fruitCommentDto 水果评论对象
     * @return
     */
    @PostMapping
    public Object saveComment(@RequestBody FruitCommentDto fruitCommentDto){
        FruitCommentInfo fruitCommentInfo = new FruitCommentInfo();
        fruitCommentInfo.setUserId(fruitCommentDto.getUserId());
        fruitCommentInfo.setFruitOrderId(fruitCommentDto.getFruitOrderId());
        fruitCommentInfo.setFruitCommentValue(fruitCommentDto.getCommentValue());
        fruitCommentInfo.setFruitCommentTime(new Date());
        if(null != fruitCommentDto.getFruitCommentId()){
            //修改水果评论
            FruitCommentInfo commentInfo = commentService.get(fruitCommentDto.getFruitCommentId());
            commentInfo.setFruitCommentValue(fruitCommentDto.getCommentValue());
            commentService.update(fruitCommentInfo);
        }else {
            //新增水果评论
            //根据订单id获取到商品id
            FruitUserOrderInfo userOrderInfo = orderService.get(fruitCommentDto.getFruitOrderId());
            fruitCommentInfo.setFruitShelfId(userOrderInfo.getFruitShelfId());
            commentService.insert(fruitCommentInfo);
            //评论完成修改订单状态为已完成
            userOrderInfo.setUserOrderStatus(FruitUserOrderStatusEnum.FINISHED.getCode());
            orderService.updateOrderInfo(userOrderInfo);
        }
        return Rets.success();
    }

    /**
     * 删除评论
     * @param commentId 评论id
     * @return
     */
    @DeleteMapping
    public Object deleteComment(@RequestParam Long commentId){
        commentService.delete(commentId);
        return Rets.success();
    }
}
